<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从数组中随机取出一个或多个单元</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-rand" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_rand</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_rand</span> &mdash; <span class="dc-title">
     从数组中随机取出一个或多个单元
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-rand-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>array_rand</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$num</code><span class="initializer"> = 1</span></span>
     ] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>

    <p class="para rdfs-comment">
     从数组中取出一个或多个随机的单元，并返回随机条目的一个或多个键。
     它使用了伪随机数产生算法，所以不适合密码学场景，
    </p>
   </div>

 <div class="refsect1 parameters" id="refsect1-function.array-rand-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">num</code></dt>

     <dd>

      <p class="para">
       指明了你想取出多少个单元。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-rand-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果只取出一个，<span class="function"><strong>array_rand()</strong></span>
   返回随机单元的键名。
   否则就返回包含随机键名的数组。
   完成后，就可以根据随机的键获取数组的随机值。
   取出数量如果超过 array 的长度，就会导致 <strong><code>E_WARNING</code></strong> 错误，并返回 NULL。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.array-rand-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.1.0</td>
       <td>
        <a href="migration71.incompatible.html#migration71.incompatible.rand_srand_aliases" class="link">内置的随机数生成算法从 libc rand 函数改成<a href="http://www.math.sci.hiroshima_u.ac.jp/~m_mat/MT/emt.html" class="link external">&raquo;&nbsp;梅森旋转</a> 伪随机数生成算法。</a>
       </td>
      </tr>

      <tr>
       <td>5.2.10</td>
       <td>
        The resulting array of keys is no longer shuffled.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-rand-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6058">
    <p><strong>Example #1 <span class="function"><strong>array_rand()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"Neo"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Morpheus"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Trinity"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Cypher"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Tank"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rand_keys&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_rand</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">$rand_keys</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">$rand_keys</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-rand-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="shuffle.html" class="function" rel="rdfs-seeAlso">shuffle()</a> - 打乱数组</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>